import numpy as np
import cv2
from matplotlib import pyplot as plt

src = cv2.imread('../IMAGES/water_coins.jpg')
# src = cv2.imread('../IMAGES/pukepai.jpeg')
img = src.copy()

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

# noise removal
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)

# sure background area
sure_bg = cv2.dilate(opening,kernel,iterations=3)

# Finding sure foreground area
# 距离变换的基本含义是计算一个图像中非零像素点到最近的零像素点的距离，也就是到零像素点的最短距离
# 个最常见的距离变换算法就是通过连续的腐蚀操作来实现，腐蚀操作的停止条件是所有前景像素都被完全
# 腐蚀。这样根据腐蚀的先后顺序，我们就得到各个前景像素点到前景中心??像素点的
# 距离。根据各个像素点的距离值，设置为不同的灰度值。这样就完成了二值图像的距离变换
#cv2.distanceTransform(src, distanceType, maskSize)
# 第二个参数 0,1,2 分别表示 CV_DIST_L1, CV_DIST_L2 , CV_DIST_C
dist_transform = cv2.distanceTransform(opening,1,5)

ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0)

# Finding unknown region
sure_fg = np.uint8(sure_fg)
unknown = cv2.subtract(sure_bg,sure_fg)

# Marker labelling
ret, markers1 = cv2.connectedComponents(sure_fg)

# Add one to all labels so that sure background is not 0, but 1
markers = markers1+1
# Now, mark the region of unknown with zero
markers[unknown==255] = 0

# 结果使用 JET 颜色地图表示。深蓝色区域为未知区域。肯定是硬币的区域使用不同的颜色标记。其余区域就是用浅蓝色标记的背景了。
# 现在标签准备好了。到最后一步：实施分水岭算法了。标签图像将会被修改，边界区域的标记将变为 -1.
markers3 = cv2.watershed(img,markers)

img[markers3 == -1] = [0,0,255]

plt.subplot(241)
plt.imshow(cv2.cvtColor(src, cv2.COLOR_BGR2RGB))
plt.title("original")
plt.axis("off")

plt.subplot(242)
plt.imshow(thresh, cmap='gray')
plt.title("thresh")
plt.axis("off")

plt.subplot(243)
plt.imshow(sure_bg, cmap='gray')
plt.title("dilate")
plt.axis("off")

plt.subplot(244)
plt.imshow(dist_transform, cmap='gray')
plt.title("dist_transform")
plt.axis("off")

plt.subplot(245)
plt.imshow(sure_fg, cmap='gray')
plt.title("thresh")
plt.axis("off")

plt.subplot(246)
plt.imshow(unknown, cmap='gray')
plt.title("unknown")
plt.axis("off")

plt.subplot(247)
plt.imshow(np.abs(markers), cmap='jet')
plt.title("markers")
plt.axis("off")

plt.subplot(248)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title("result")
plt.axis("off")

plt.show()
